package com.haoan.index.controller;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.haoan.common.annotation.Anonymous;
import com.haoan.index.common.WebConstants;
import com.haoan.index.common.enums.ValiFlag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haoan.common.annotation.Log;
import com.haoan.common.core.controller.BaseController;
import com.haoan.common.core.domain.AjaxResult;
import com.haoan.common.enums.BusinessType;
import com.haoan.index.domain.Postcard;
import com.haoan.index.service.PostcardService;
import com.haoan.common.utils.poi.ExcelUtil;
import com.haoan.common.core.page.TableDataInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 明信片Controller
 *
 * @author autoGen
 * @date 2025-02-19
 */
@Api(value="明信片",tags = "明信片")
@RestController
@RequestMapping("/index/postcard")
public class PostcardController extends BaseController
{
    @Resource
    private PostcardService postcardService;

    /**
     * 官网查询明信片
     */
    @ApiOperation("查询明信片列表")
    @GetMapping("/getOne")
    @Anonymous
    public AjaxResult getOne(){
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 创建一个日期格式化器，指定格式为yyyy-MM-dd
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 使用格式化器将日期转换为字符串
        String formattedDate = currentDate.format(formatter);
        LambdaQueryWrapper<Postcard> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Postcard::getDay,formattedDate);
        queryWrapper.eq(Postcard::getValiFlag, ValiFlag.VALID);
        queryWrapper.orderByDesc(Postcard::getCreateTime);
        queryWrapper.last("limit 1");
        Postcard postcard = postcardService.getOne(queryWrapper);
        return success(postcard);
    }

    /**
     * 查询明信片列表
     */
    @ApiOperation("查询明信片列表")
    @PreAuthorize("@ss.hasPermi('index:postcard:list')")
    @GetMapping("/list")
    public TableDataInfo list(Postcard postcard)
    {
        startPage();
        List<Postcard> list = postcardService.selectPostcardList(postcard);
        return getDataTable(list);
    }

    /**
     * 导出明信片列表
     */
    @ApiOperation("导出明信片列表")
    @PreAuthorize("@ss.hasPermi('index:postcard:export')")
    @Log(title = "明信片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Postcard postcard)
    {
        List<Postcard> list = postcardService.selectPostcardList(postcard);
        ExcelUtil<Postcard> util = new ExcelUtil<Postcard>(Postcard.class);
        util.exportExcel(response, list, "明信片数据");
    }

    /**
     * 获取明信片详细信息
     */
    @ApiOperation("获取明信片详细信息")
    @PreAuthorize("@ss.hasPermi('index:postcard:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(postcardService.selectPostcardById(id));
    }

    /**
     * 新增明信片
     */
    @ApiOperation("新增明信片")
    @PreAuthorize("@ss.hasPermi('index:postcard:add')")
    @Log(title = "明信片", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Postcard postcard)
    {
        return toAjax(postcardService.insertPostcard(postcard));
    }

    /**
     * 修改明信片
     */
    @ApiOperation("修改明信片")
    @PreAuthorize("@ss.hasPermi('index:postcard:edit')")
    @Log(title = "明信片", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Postcard postcard)
    {
        return toAjax(postcardService.updatePostcard(postcard));
    }

    /**
     * 删除明信片
     */
    @ApiOperation("删除明信片")
    @PreAuthorize("@ss.hasPermi('index:postcard:remove')")
    @Log(title = "明信片", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(postcardService.deletePostcardByIds(ids));
    }
}
